<script setup>
import * as api from "@/api/v-sys-service/sys-manage/dict/index.js";
import dictItemsFormDialog from "./dict-items-form-dialog.vue"

const emits = defineEmits([
  'query',
  'update:modelValue'
])

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => {}
  }
})


const visible = computed(
    {
      get(){
        return props.modelValue
      },
      set(val){
        emits('update:modelValue',val)
      }
    }
)

/**
 * 组件挂载后
 */
onMounted(() => {
  init()
})

/**
 * 列表数据
 */
const tableData = ref([])

/**
 * loading状态
 */
const loading = ref({
  tableLoading: false,// 列表loading
  formBtnLoading: false,// 保存按钮loading
})

/**
 * 弹窗显隐
 */
const dialog = ref({
  showFormDialog: false, //表单弹窗
})

/**
 * 弹窗标题
 */
const title = ref("")

const itemData = ref({})
watch(
    props.rowData,
    (val) =>{
      if (val && val.id) title.value = "" + val.name + " 字典数据"
    },
    {
      immediate: true,
    }
)

/**
 * 计算表格属性
 */
const tableInfo = computed(()=>{
  const tableProps = {
    data: tableData.value,
    loading: loading.value.tableLoading,
    checkConfig: { highlight: true },
    showSelectCount: false,
    showExportBtn: false,
  }
  return { ...tableProps }
})

/**
 * 分页参数
 */
const pageQuery = reactive({
  page: 1,
  limit: 50,
  total: 0,
  orders: [],
  filters: [],
  condition: {
    key: "",
    typeCode: props.rowData.code
  },
})

/**
 * 组件初始化调用函数
 */
const init = () => {
  init_PageQuery()
}

/**
 * 分页查询
 */
const init_PageQuery = async () => {
  loading.value.tableLoading = true
  const { code, data } = await api.list(pageQuery)
  if (code === 200) {
    loading.value.tableLoading = false
    tableData.value = data.records
    pageQuery.total = data.total
  }
}

/**
 * form表单
 */
const showFormDialog = ($event,row) => {
  if (row && row.id) itemData.value = row
  else itemData.value = row || {}
  dialog.value.showFormDialog = true
}

/**
 * 删除
 */
const showRemoveDialog = ($event,row) => {
  ElMessageBox.confirm('', '确定删除吗',
      {
        closeOnClickModal: false,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '拼命加载中...'
            api.remove(row.id).then((res)=>{
              instance.confirmButtonLoading = false
              if (code === 200){
                ElMessage({
                  message: '删除成功',
                  type: 'success',
                })
                init_PageQuery()
              }
            })
            done()
          } else {
            done()
          }
        }
      }
  )
}

/**
 * 组件对外提供可访问数据
 */
defineExpose({
  showFormDialog,
  showRemoveDialog,
})

</script>

<template>
  <div class="dict-box flex flex-col">
    <div class="search flex align-center">
      <div class="back" @click="visible = false">
        {{'\u2002'}}
        <div class="icon">
          <img src="@/assets/back.png" alt="">
        </div>
        <p>返回</p>
      </div>
      <label class="label">{{'\u2002'}}关键字{{'\u2003'}}</label>
      <el-input
          v-model="pageQuery.condition.key"
          style="width: 180px;" placeholder="请输入关键字"
          @keyup.enter="init_PageQuery" @clear="init_PageQuery" clearable
      />
      <div class="el-button" style="display: none"></div>
      <el-button type="primary" @click="init_PageQuery">
        <template #icon><i-ep-search /></template>搜索
      </el-button>
      <el-button type="success" @click="showFormDialog">
        <template #icon><i-ep-plus /></template>新增
      </el-button>
    </div>
    <v-table
        v-model="pageQuery"
        :table-info="tableInfo"
        :column-info="api.columnInfo"
        @query="init_PageQuery"
        margin-bottom-height="300"
    >
      <template #status="{row}">
        <el-tag v-if="row.status" type="success">启用</el-tag>
        <el-tag v-else type="danger">禁用</el-tag>
      </template>
    </v-table>
    <dict-items-form-dialog v-model="dialog.showFormDialog" v-if="dialog.showFormDialog" @query="init_PageQuery" :parent-data="rowData" :row-data="itemData"/>
  </div>
</template>

<style scoped lang="scss">
.dict-box {
  padding: 10px;
  :deep( .container) {
    .el-button {
      padding: 0;
    }
  }

  .back {
    width: 50px;
    display: flex;
    align-items: center;
    .icon {
      width: 8px !important;
      height: 16px !important;
      line-height: 16px !important;
      margin-right: 6px;
      margin-top: 2px;
      cursor: pointer;
      img {
        width: 100%;
        height: 100%;
      }
    }
    p {
      vertical-align: middle;
      height: 20px;
      line-height: 20px;
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #00a7ff;
      cursor: pointer;
    }
  }
}
</style>
